//
//  ReadBookSetView.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/7.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "ReadBookSetView.h"

@implementation ReadBookSetView

- (instancetype)initWithFrame:(CGRect)frame
{
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = CSColorFromRGBA(0x333333,0);
        [self addSubview:alphaView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenWithAnimation)];
        [alphaView addGestureRecognizer:tap];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = CSColorFromRGB(0x333333);
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        /**
         亮度减少
         */
        UIButton *minLightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [minLightBtn setImage:[UIImage imageNamed:@"光线减"] forState:UIControlStateNormal];
        minLightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        minLightBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        minLightBtn.imageView.layer.masksToBounds = YES;
        [whiteView addSubview:minLightBtn];
        self.minLightBtn = minLightBtn;
        
        /**
         亮度增加
         */
        UIButton *moreLightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [moreLightBtn setImage:[UIImage imageNamed:@"光线加"] forState:UIControlStateNormal];
        moreLightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        moreLightBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        moreLightBtn.imageView.layer.masksToBounds = YES;
        [whiteView addSubview:moreLightBtn];
        self.moreLightBtn = moreLightBtn;
        
        /**
         亮度
         */
        UISlider *lightSlider = [[UISlider alloc]init];
        self.LightSlider = lightSlider;
        lightSlider.minimumValue = 0;
        lightSlider.maximumValue = 1;
        lightSlider.value = [[UIScreen mainScreen] brightness];
        [lightSlider addTarget:self action:@selector(changeLight:) forControlEvents:UIControlEventValueChanged];
        lightSlider.minimumTrackTintColor = CSColorFromRGB(0xF95D48);
        lightSlider.maximumTrackTintColor = CSWhiteColor;
        lightSlider.thumbTintColor = CSWhiteColor;
        [whiteView addSubview:lightSlider];
        
   
        /**
         行间距
         */
        UILabel *distancLable = [[UILabel alloc] init];
        distancLable.text = @"行间距";
        distancLable.font = SmallFont;
        distancLable.textColor = CSWhiteColor;
        [whiteView addSubview:distancLable];
        self.distancLable = distancLable;
        
        /**
         行间距
         */
        UISlider *distancSlider = [[UISlider alloc]init];
        self.distancSlider = distancSlider;
        distancSlider.minimumValue = 5;
        distancSlider.maximumValue = 20;
        distancSlider.value = [LSYReadConfig shareInstance].lineSpace;
        distancSlider.minimumTrackTintColor = CSColorFromRGB(0xF95D48);
        distancSlider.maximumTrackTintColor = CSWhiteColor;
        distancSlider.thumbTintColor = CSWhiteColor;
        [whiteView addSubview:distancSlider];
        
        /**
         字体大小
         */
        UILabel *fontLable = [[UILabel alloc] init];
        fontLable.text = @"字号";
        fontLable.font = SmallFont;
        fontLable.textColor = CSWhiteColor;
        [whiteView addSubview:fontLable];
        self.fontLable = fontLable;
        
        /**
         字体小
         */
        UIButton *minFontBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [minFontBtn setImage:[UIImage imageNamed:@"字体减"] forState:UIControlStateNormal];
        minFontBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        minFontBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        minFontBtn.imageView.layer.masksToBounds = YES;
        minFontBtn.layer.cornerRadius = 5;
        minFontBtn.layer.borderWidth = 1;
        minFontBtn.layer.borderColor = CSWhiteColor.CGColor;
        minFontBtn.clipsToBounds = YES;
        [whiteView addSubview:minFontBtn];
        self.minFontBtn = minFontBtn;
        
        /**
         字体大
         */
        UIButton *moreFontBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [moreFontBtn setImage:[UIImage imageNamed:@"字体加"] forState:UIControlStateNormal];
        moreFontBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        moreFontBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        moreFontBtn.imageView.layer.masksToBounds = YES;
        moreFontBtn.layer.cornerRadius = 5;
        moreFontBtn.layer.borderWidth = 1;
        moreFontBtn.layer.borderColor = CSWhiteColor.CGColor;
        moreFontBtn.clipsToBounds = YES;
        [whiteView addSubview:moreFontBtn];
        self.moreFontBtn = moreFontBtn;
        
        [self setConstraints];
        
    }
    
    return self;
}

-(void)setConstraints
{
    
    
    
    /**
     背景色
     */
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    /**
     内容view
     */
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(180 + TabBarHeightIphonexSafe);

    }];
    
    /**
     亮度减少
     */
    [self.minLightBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.whiteView).offset(30);
        make.top.mas_equalTo(self.whiteView).offset(25);
        
    }];
    
    /**
     亮度增加
     */
    [self.moreLightBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.whiteView).offset(-25);
        make.centerY.mas_equalTo(self.minLightBtn);
        
    }];
    
    /**
     亮度
     */
    [self.LightSlider mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.minLightBtn.mas_right).offset(20);
        make.right.mas_equalTo(self.moreLightBtn.mas_left).offset(-20);
        make.centerY.mas_equalTo(self.minLightBtn);
        make.height.mas_equalTo(20);
        
    }];
    
    /**
     行间距
     */
    [self.distancLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.minLightBtn.mas_bottom).offset(40);
        make.centerX.mas_equalTo(self.minLightBtn);
        
    }];
    
    /**
     行间距
     */
    [self.distancSlider mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.LightSlider);
        make.width.mas_equalTo(self.LightSlider);
        make.centerY.mas_equalTo(self.distancLable);
        make.height.mas_equalTo(20);
        
    }];
    
    /**
     字体大小
     */
    [self.fontLable mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(self.distancLable.mas_bottom).offset(42);
        make.right.mas_equalTo(self.distancLable);


    }];

    /**
     字体小
     */
    [self.minFontBtn mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self.fontLable.mas_right).offset(20);
        make.centerY.mas_equalTo(self.fontLable);
        make.width.mas_equalTo(Iphone5?90:105);
        make.height.mas_equalTo(30);

    }];

    /**
     字体大
     */
    [self.moreFontBtn mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self.minFontBtn.mas_right).offset(28);
        make.centerY.mas_equalTo(self.fontLable);
        make.width.mas_equalTo(Iphone5?90:105);
        make.height.mas_equalTo(30);

    }];


    
    
    
}


- (void)showWithAnimation {
    
    
    
    [self addViews];
    
    CGRect frame = self.whiteView.frame;
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    self.whiteView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.whiteView.y = frame.origin.y;
    }];
    
}


- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    
    [self removeFromSuperview];
}




#pragma mark - click

//亮度
-(void)changeLight:(UISlider *)sender
{
     [[UIScreen mainScreen] setBrightness: sender.value];
    
}

////行间距
//-(void)changeFont:(UISlider *)sender
//{
//    [LSYReadConfig shareInstance].fontSize = 15;
//
//    if ([self.delegate respondsToSelector:@selector(ReadBookSetViewFontSize:)]) {
//        [self.delegate ReadBookSetViewFontSize:self];
//    }
//}

@end
